package com.sanerzone.smscenter.biz.VerifyInterface.impl;

import com.sanerzone.smscenter.biz.VerifyInterface.GatewayMatch;
import com.sanerzone.smscenter.biz.VerifyInterface.data.ResultData;
import com.sanerzone.smscenter.biz.VerifyInterface.data.Sort;
import com.sanerzone.smscenter.biz.VerifyInterface.util.FilterUtil;
import com.sanerzone.smscenter.biz.VerifyInterface.util.MatchUtil;
import com.sanerzone.smscenter.biz.cache.GatewayRelHelper2;
import com.sanerzone.smscenter.biz.cache.PhoneHelper;
import com.sanerzone.smscenter.biz.entity.SmsGatewayGroupRel;
import com.sanerzone.smscenter.biz.iface.DataAccess.DataAccess;
import com.sanerzone.smscenter.biz.message.SMSMTMessage;
import com.sanerzone.smscenter.biz.message.SMSREQMessage;

import java.util.*;

public class GatewayMatchImpl implements GatewayMatch{

    /**
     * 为了区分出哪些独立的(地区+运营商)有哪些号码
     * @param groupId
     * @param phone
     * @param result
     * @param reqMsg
     * @return
     */
    @Override
    public ResultData findGateway( String groupId,String phone,List<SMSMTMessage> result,SMSREQMessage reqMsg,DataAccess dataAccess) {
        ResultData resultData=new ResultData();

        //经过分配的订单  key为 phoneType+"_"+provinceId  **
        //这个key只是确定一个组，一个key对应下可能还有很多个网关
        Map<String,StringBuffer> afterDispOrders=new HashMap<>();
        Set<String> keys=new HashSet<>( );
        //拿到这个 网管组 下的所有网关关系

        List<SmsGatewayGroupRel> smsGatewayGroupRels=dataAccess.getGatewayRelByGroup( groupId );
        for(SmsGatewayGroupRel smsGatewayGroupRel:smsGatewayGroupRels){
            String phoneType=smsGatewayGroupRel.getPhoneType();
            String provinceId= smsGatewayGroupRel.getProvinceId();
            String key=phoneType+"_"+provinceId;
            String[] phones= phone.split( "," );
            if (phone.equals( "" )){
                break;
            }
            if (!keys.contains(key) ){
                int offset=0;
                for (int i=0;i<phones.length;i++){
                    String phoneTemp=phones[i];
                    //拿到该号码的运营商和地区
                    Map<String, String> phoneMap = PhoneHelper.get(phoneTemp);
                    String phoneTypeFind = PhoneHelper.getPhoneType(phoneMap);//运营商
                    String cityCodeFind = PhoneHelper.getCityCode(phoneMap);//省市代码
                    String keyFind=phoneTypeFind+"_"+cityCodeFind.substring( 0,2 );
                    StringBuffer sb= afterDispOrders.get( keyFind );
                    //如果找到的key 而且目前没有相同的key 等于正在遍历的网关关系的key
                    if(key.equals(keyFind)){
                        //找到，已经成功找到了运营商、地区网关小组 剔除
                        keys.add( key );
                        phone=FilterUtil.removePhone( phoneTemp,phone,i-offset );
                        offset++;
                        if (sb==null){
                            //如果为空
                            afterDispOrders.put( key,new StringBuffer(phoneTemp ) );
                        }else{
                            sb.append( ","+phoneTemp );
                        }
                    }

                }


            }


        }
        //如果经过筛选还是有剩下的
        if (phone.length()>0){
            MatchUtil.groupingNationGateway( afterDispOrders,phone,smsGatewayGroupRels,reqMsg,result );
        }
        //过滤一层不能群发的
       List<Sort> sorts= MatchUtil.filterMassSendOrNot(afterDispOrders,groupId,result,reqMsg,dataAccess);

       if (sorts.size()>0){
           //过滤掉一层不能小于最小发送量的
           sorts=MatchUtil.filterLessThanMinNum(sorts,result,reqMsg);

           //        //提出最高的优先级
           //        sorts= MatchUtil.filterBestPriority( sorts);

           if (sorts.size()>0){
               //权重筛选
               sorts=MatchUtil.weightChoose( sorts,result,reqMsg );
//                //根据白名单重排序
//               MatchUtil.reSortsByWhiteList(sorts);
               //分批处理
               MatchUtil.sendInBatches(sorts,result,reqMsg,groupId);

           }

        }

        resultData.setResult( result );
        return resultData;
    }

}
